package com.lcicl.reggie.service.impl;
/*
  Date: 2023/09/13
  Author: Luochao
  Description:
*/

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lcicl.reggie.controller.CommonController;
import com.lcicl.reggie.entity.Setmeal;
import com.lcicl.reggie.entity.SetmealDish;
import com.lcicl.reggie.entity.dto.SetmealDto;
import com.lcicl.reggie.mapper.SetmealMapper;
import com.lcicl.reggie.service.SetmealDishService;
import com.lcicl.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    SetmealDishService setmealDishService;
    @Autowired
    CommonController commonController;

    /**
     * 保存套餐
     * @param setmealDto
     */
    @Override
    public void saveSetmealDto(SetmealDto setmealDto) {
        this.save(setmealDto);

        Long setmealId = setmealDto.getId();
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().forEach(item->{
            item.setSetmealId(setmealId);
        });
        setmealDishService.saveBatch(setmealDishes);

    }

    /**
     * 套餐数据回显
     * @param id
     * @return
     */
    @Override
    public SetmealDto getSetmealDto(Long id) {

        Setmeal setmeal = this.getById(id);         //查询套餐数据
        SetmealDto setmealDto = new SetmealDto();

        BeanUtils.copyProperties(setmeal, setmealDto);  //setmeal拷贝到setmealDto

        //查询套餐菜品
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);

        setmealDto.setSetmealDishes(list);  //套餐菜品信息加入Dto
        System.out.println(setmealDto.toString());

        return setmealDto;
    }


    /**
     * 套餐更新
     * @param setmealDto
     */
    @Override
    public void updateSetmealDto(SetmealDto setmealDto) {
        this.updateById(setmealDto);

        Long setmealId = setmealDto.getId();
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, setmealId);
        setmealDishService.remove(queryWrapper);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().forEach(i->{
            i.setSetmealId(setmealId);
        });

        setmealDishService.saveBatch(setmealDishes);
    }


    /**
     * 删除套餐
     * @param ids
     */
    @Override
    public boolean deleteSetmeals(ArrayList<Long> ids) {
        ids.stream().forEach(id->{
            LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SetmealDish::getSetmealId, id);
            setmealDishService.remove(queryWrapper);            //删套餐菜品

            Setmeal setmeal = this.getById(id);
            commonController.delete(setmeal.getImage());        //删图片
        });
        boolean b = this.removeByIds(ids);

        return b;
    }
}
